<template>
  <div class="topbar">
    <div class="left">
      <button class="menu-toggle" @click="$emit('toggle-menu')">
        <i class="fas fa-bars"></i>
      </button>
      <div class="breadcrumb">
        <span>{{ currentTitle }}</span>
      </div>
    </div>
    <div class="right">
      <div class="search-box">
        <i class="fas fa-search"></i>
        <input type="text" placeholder="搜索..." />
      </div>
      <div class="notifications">
        <i class="fas fa-bell"></i>
        <span class="badge">3</span>
      </div>
      
      <el-dropdown @command="handleCommand" trigger="click">
        <div class="user-info">
          <img :src="avatarUrl" alt="Avatar" />
          <span>{{ username || '管理员' }}</span>
          <i class="fas fa-chevron-down"></i>
        </div>
        
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="profile">
              <el-icon><User /></el-icon> 个人信息
            </el-dropdown-item>
            <el-dropdown-item command="settings">
              <el-icon><Setting /></el-icon> 系统设置
            </el-dropdown-item>
            <el-dropdown-item divided command="logout">
              <el-icon><SwitchButton /></el-icon> 退出登录
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script setup>
import { computed, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { User, Setting, SwitchButton } from '@element-plus/icons-vue'
import { ElMessageBox } from 'element-plus'
import defaultAvatar from '@/assets/avatar.svg'

const router = useRouter()
const route = useRoute()
const currentTitle = computed(() => route.meta.title || '首页')

// 从localStorage获取用户信息
const username = ref(localStorage.getItem('username') || '管理员')
const avatarUrl = ref(localStorage.getItem('avatar') || defaultAvatar)

// 处理下拉菜单选项点击
const handleCommand = (command) => {
  switch (command) {
    case 'profile':
      router.push('/profile')
      break
    case 'settings':
      // 暂时不实现
      break
    case 'logout':
      ElMessageBox.confirm(
        '确定要退出登录吗？',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        // 清除登录信息
        localStorage.removeItem('token')
        localStorage.removeItem('userId')
        localStorage.removeItem('username')
        // 重定向到登录页
        router.push('/login')
      }).catch(() => {})
      break
  }
}
</script>

<style scoped>
.topbar {
  height: 60px;
  background: #fff;
  border-bottom: 1px solid #e6e6e6;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.left {
  display: flex;
  align-items: center;
}

.menu-toggle {
  background: none;
  border: none;
  font-size: 20px;
  color: #666;
  cursor: pointer;
  padding: 8px;
  margin-right: 16px;
  transition: color 0.3s ease;
}

.menu-toggle:hover {
  color: #ffd04b;
}

.breadcrumb {
  font-size: 16px;
  color: #333;
}

.right {
  display: flex;
  align-items: center;
  gap: 20px;
}

.search-box {
  display: flex;
  align-items: center;
  background: #f5f5f5;
  border-radius: 20px;
  padding: 6px 16px;
}

.search-box i {
  color: #999;
  margin-right: 8px;
}

.search-box input {
  border: none;
  background: none;
  outline: none;
  width: 200px;
  font-size: 14px;
}

.notifications {
  position: relative;
  cursor: pointer;
}

.notifications i {
  font-size: 18px;
  color: #666;
}

.badge {
  position: absolute;
  top: -8px;
  right: -8px;
  background: #ff4d4f;
  color: #fff;
  font-size: 12px;
  padding: 2px 6px;
  border-radius: 10px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 4px;
  transition: background-color 0.3s ease;
}

.user-info:hover {
  background: #f5f5f5;
}

.user-info img {
  width: 32px;
  height: 32px;
  border-radius: 50%;
}

.user-info span {
  font-size: 14px;
  color: #333;
}

.user-info i {
  font-size: 12px;
  color: #999;
}
</style> 